<?php

require_once 'core.php';
require_once cf\Config::path.'api/admin.php';

$tpl = 'edit_news';

function do_edit_save($aid, $params) {
	$newsId = cf\api\admin\doAction($aid, $params);
	if (array_key_exists('id',$params)) {
		$newsId = $params['id'];
	}
			//update categories
	cf\execQuery('DELETE FROM cf_news_in_category WHERE news_id=:id',array('id'=>$newsId));
	$cats = cf\param::exist('cat') ? cf\param::get('cat') : array();
	$q = cf\createQuery('INSERT INTO cf_news_in_category (category_id,news_id) VALUES(:catId,:newsId)',array('newsId'=>$newsId));
	foreach ($cats as $catId) {
		$q->setParam('catId',$catId);
		$q->execute();
	}
}

function do_edit_create($k) {
	global $smarty;
	$smarty->assign('allCategories',json_encode(cf\api\admin\getTree('news_categories')));
	$smarty->assign(
		'newsCategories',
		json_encode(
			cf\query2vector('SELECT category_id FROM cf_news_in_category WHERE news_id=:newsId', array('newsId'=>$k ? $k['id'] : 0))
		)
	);
}

require_once 'edit.php';
?>